package com.kingint.web.portal.serve.controller;


import com.kingint.mbg.dto.MemberReturn;
import com.kingint.mbg.dto.department.DepartmentRepairFront;
import com.kingint.mbg.dto.department.DepartmentRepairParam;
import com.kingint.mbg.group.Insert;
import com.kingint.mbg.group.Update;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.kingint.common.unitedresult.K;

import com.kingint.web.portal.serve.service.DepartmentRepairService;

import javax.annotation.Resource;
import java.util.List;


/**
 * @描述：部门保修表控制类
 * @作者: wxh
 * @日期: 2020-12-20
 */
@RestController
@RequestMapping("/departmentRepair")
@Api(value = "DepartmentRepairController", description = "前端部门保修表控制层")
@CrossOrigin
public class DepartmentRepairController {

    @Resource
    public DepartmentRepairService serDepartmentRepairService;

    @ApiOperation(value = "添加部门保修表")
    @PostMapping("/create")
    public Object create(@Validated(Insert.class) @RequestBody DepartmentRepairParam departmentRepairParam) {
        int i = serDepartmentRepairService.addDepartmentRepairInfo(departmentRepairParam);
        if (i > 0) {
            return K.ok().data(i);
        } else if (i == -1) {
            return K.error().data("该部门没有经理，无法添加工单");
        } else {
            return K.error();
        }
    }

    @ApiOperation(value = "维修人员接单,传递id")
    @GetMapping("/receiveOrder")
    public Object receiveOrder(@RequestParam(value = "id") Integer id) {
        Integer integer = serDepartmentRepairService.receiveOrder(id);
        if (integer > 0) {
            return K.ok().data(integer);
        }
        return K.error();
    }

    @ApiOperation(value = "获取所有待接单维修单")
    @GetMapping("/getWaitReceiveOrder")
    public Object getWaitReceiveOrder() {
        List<DepartmentRepairFront> waitReceiveOrder = serDepartmentRepairService.getWaitReceiveOrder();
        return K.ok().data(waitReceiveOrder);
    }

    @ApiOperation(value = "获取对应维修单的详细信息")
    @GetMapping("/departmentRepairOrderDetails")
    public Object getDepartmentRepairOrderDetails(@RequestParam(value = "workId") Integer workId) {
        DepartmentRepairParam param = serDepartmentRepairService.getDepartmentRepairOrderDetails(workId);
        return K.ok().data(param);
    }

    @ApiOperation(value = "维修人员完成维修提交维修单")
    @PostMapping("/finishDepartmentRepairOrder")
    public Object finishDepartmentRepairOrder(@Validated(Update.class) @RequestBody DepartmentRepairParam departmentRepairParam) {
        int i = serDepartmentRepairService.finishDepartmentRepairOrder(departmentRepairParam);
        if (i > 0) {
            return K.ok().data(i);
        }
        return K.error();
    }

    @ApiOperation(value = "查询用户处理中的维修单列表")
    @PostMapping("/getUserHandleRepairOrder")
    public Object getUserHandleRepairOrder() {
        List<DepartmentRepairFront> list = serDepartmentRepairService.getUserHandleRepairOrder();
        return K.ok().data(list);
    }

    @ApiOperation(value = "查询用户待验收的维修单")
    @PostMapping("/getUserWaitCheckOrder")
    public Object getUserWaitCheckOrder() {
        List<DepartmentRepairFront> list = serDepartmentRepairService.getUserWaitCheckOrder();
        return K.ok().data(list);
    }

    //查询用户的历史记录
    @ApiOperation(value = "查询用户已经完成的维修单表")
    @PostMapping("/getAlreadyComplete")
    public Object getAlreadyCompleted() {
        List<DepartmentRepairFront> list = serDepartmentRepairService.getAlreadyCompleted();
        return K.ok().data(list);
    }

    @ApiOperation(value = "获取维修人员列表")
    @GetMapping("/getRepairUserList")
    public Object getRepairUserList() {
        List<MemberReturn> list = serDepartmentRepairService.getRepairUserList();
        return K.ok().data(list);
    }

    @ApiOperation(value = "验收人员验收工单,id为workId")
    @GetMapping("/completeRepairInfo")
    public Object completeRepairInfo(@RequestParam(value = "status", defaultValue = "0") Integer status,
                                     @RequestParam("id") Integer id) {
        Integer i= serDepartmentRepairService.completeRepairInfo(status,id);
        if(i>0){
            return K.ok().data(i);
        }
        return K.error();
    }
}